//
//  WPNetManager+Extension.swift
//  WB
//
//  Created by 汪鹏 on 2016/12/25.
//  Copyright © 2016年 汪鹏. All rights reserved.
//

import Foundation


// MARK: - 封装微博数据请求
extension WPNetManager {
    
    /// 微博数据请求
    func statusList(since_id:Int64, max_id:Int64, completion: @escaping (_ result:[[String: Any]]?, _ isSuccess: Bool) -> ()){
      
        
        let url = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let parameters = [
            "since_id":since_id,
            "max_id":max_id > 0 ? max_id - 1 : max_id
        ]
        
        requestWithToken(method: .GET, URLString: url, parameters: parameters){ (json, isSuccess) -> () in
            
            let res = json as? [String:Any]
            
            guard let result = res?["statuses"] as? [[String:Any]]else{
                
                print(#function+"微博数据请求失败")
                
                completion(nil,true)
                
                return
            }
            // 微博数据的回调
            completion(result,true)
        }
        
    }
    
    
    
    /// 读取用户未读的微博数量
    func getUnReadStatusCount(completion: @escaping (_ count: Int) -> ()) {
        
        let url = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
        // 拼接参数
        let parameters = [
            "uid":userAccount.access_token
        ]
        
        requestWithToken(URLString: url, parameters: parameters){ (json, isSuccess) in
            
            // 去数据
            let json = json as? [String:Any]
            
            guard let count = json?["status"] as? Int else{
                
                print(#function+"读取用户未读的微博数量失败")
                
                return
            }
            
            // 成功的回调
            completion(count)
        }

    }
    
}

// MARK:-- 获取用户token
extension WPNetManager {
    
    func loadAccessToken(code:String, completion: @escaping (_ isSuccess: Bool) -> ()) {
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let parameters = ["client_id":APP_KEY,
                          "client_secret":APP_SECRET,
                          "grant_type":"authorization_code",
                          "code":code,
                          "redirect_uri":APP_REDIRECTURL
                          ]
        
        request(method: .POST, URLString: urlString, parameters: parameters) { (json, isSuccess) in
            
            // 字典转模型
            self.userAccount.yy_modelSet(with: json as? [String:Any] ?? [:])
            
            self.loadUserInfor(completion: { (dict) in
                
                self.userAccount.yy_modelSet(withJSON: dict)
                
                // 结果的回调
                completion(isSuccess)
                
                // 保存用户账户信息
                self.userAccount.saveUserAccount()
            })
            
        }
    
    }
    
}

// MARK:--获取用户信息
extension WPNetManager {
    
    func loadUserInfor(completion:@escaping (_ dict:[String:Any]) -> ()) {
       
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        let parameters = ["access_token":self.userAccount.access_token,
                          "uid":self.userAccount.uid,
                         ]
        
        requestWithToken(URLString: urlString, parameters: parameters) { (json, isSuccess) in
            
            let dict = json as? [String:Any] ?? [:]
            
            completion(dict)
        }
        
    }
    
}
